export const toastInfo = (title) => {
    wx.showToast({
        title,
        icon: 'none',
        duration: 2000
    })
}
// 暂停多少毫秒(调用的时候 要用 await)
export const sleep = ms => new Promise(r => setTimeout(r, ms));


// js 给 object 添加 扩展方法
Object.defineProperty(Object.prototype, "isSuccess", {
    value: function isSuccess() {
        return this.resultCode === "1";

    },
    writable: true,
    configurable: true
})

const baseUrl = "http://10.168.1.171:7787"
const http = (options) => {
    let token = wx.getStorageSync('token');
    return new Promise((resolve) => {
        wx.request({
            url: baseUrl + options.url,
            method: options.method || 'get',
            data: options.data || {},
            header: options.header || {
                'content-type': 'application/json',// 默认值
                'Authorization': 'Bearer ' + token,
            },
            success: result => {
                // console.log("result", result)
                let statusCode = result.statusCode;
                if (statusCode === 200) {
                    if (result.data.resultCode !== "1") {
                        toastInfo("错误:" + result.data.resultInfo)
                    }
                    resolve(result.data)
                } else {
                    if (statusCode === 401) {
                        toastInfo("请求没有提供token或登录过期,请重新登录")
                        // todo 后面 写跳转登录的代码
                        wx.redirectTo({url: '/pages/index/index'})
                    }

                    resolve({
                        resultCode: "2222"
                    })
                }
            },
            fail: err => {
                console.log("err", err)
                toastInfo("wx.request 请求错误,可能是连接超时")
                wx.redirectTo({url: '/pages/index/index'})
                resolve({
                    resultCode: "2222"
                })
            }
        })
    })
}
export const login = (username, password) => {
    return http({
        url: '/LoginService/login',
        method: 'post',
        data: {login: username, password}
    })
}
export const getUserInfo = (userId) => {
    return http({
        url: '/SysUserInfo/load',
        method: 'post',
        data: {userId}
    })
}
// 根据 基站id 获取 信息
export const getStationById = (terminalId) => {
    return http({
        url: '/operation/get/terminalState',
        method: 'post',
        data: {terminalId}
    })
}
export const getTerminalById = (guid) => {
    return http({
        url: '/SysTerminalInfo/load',
        method: 'post',
        data: {guid}
    })
}
// 获取用户列表
export const getUsers = (data) => {
    return http({
        url: '/SysUserInfo/search',
        method: 'post',
        data
    })
}
// 获取工单 列表
export const getWorkOrders = (data) => {
    return http({
        url: '/FaultService/searchFault',
        method: 'post',
        data
    })
}
// 更新 terminal state
export const updateTerminalState = (data) => {
    return http({
        url: '/operation/update/terminalState',
        method: 'post',
        data
    })
}
